<template>
  <DataTable searchPlaceholder="请输入医生相关信息" :columns="columns" dialogTitle="医生信息表" :formFields="formFields"
    :tableData="doctorData" apiUrl="doctor" :putUser="putUser" :addUser="addUser" :delUser="delUser" />
</template>

<script setup>
import DataTable from '../../components/DataTable.vue';
import { ref, onMounted } from "vue";
import axios from "axios";

// 定义表格列配置
const columns = [
  { prop: 'userName', label: '账号', width: '110' },
  { prop: 'nickName', label: '昵称', width: '110' },
  { prop: 'officeName', label: '科室', width: '110' },
  { prop: 'doctorDescribe', label: '医生简介', width: 'show-overflow-tooltip' },
];

// 定义表单字段配置
const formFields = [
  { label: '账号', model: 'userName', placeholder: '请输入账号' },
  { label: '密码', model: 'userPasswd', placeholder: '请输入密码' },
  { label: '昵称', model: 'nickName', placeholder: '请输入昵称' },
  { label: '科室', model: 'officeId', placeholder: '请选择科室' },
  { label: '医生简介', model: 'doctorDescribe', placeholder: '请输入医生简介' },
];

// 定义医生数据
const doctorData = ref([]);

// 获取所有医生信息
async function getUsers() {
  try {
    const response = await axios.get("/api/office/getAll");
    // doctorData.value = response.data.data;
    doctorData.value = response.data.data.filter(doctor => doctor.userName !== null);
    console.log("doctorData:", doctorData.value);
  } catch (error) {
    console.error("获取医生数据时出错:", error);
  }
}

// 新增数据
async function addUser(formValue) {
  console.log("new")
  console.log(formValue)

  try {
    await axios.post("api/hospital/addUser", formValue);
    console.log("doctor新增", formValue)
    location.reload()
  } catch (error) {
    console.error("新增用户时出错:", error);
  }
}

// 编辑数据
async function putUser(formValue) {
  try {
    await axios.put(`api/hospital/putUser/${formValue.doctorId}`, formValue);
    console.log("doctor编辑", formValue)
  } catch (error) {
    console.error("编辑用户时出错:", error);
  }
}

// 删除数据确
async function delUser(index, row) {
  console.log("医生信息删除：", row.doctorId)
  try {
    await axios.delete(`api/hospital/delUser/${row.doctorId}`);
    console.log("医生信息删除：", row.id)
  } catch (error) {
    console.error("删除用户时出错:", error);
  }
}

// 在组件挂载时获取医生数据
onMounted(() => {
  getUsers()
});
</script>
